package com.husong.util.sms;

import java.util.ArrayList;
import java.util.UUID;

import com.esms.MessageData;
import com.esms.PostMsg;
import com.esms.common.entity.Account;
import com.esms.common.entity.GsmsResponse;
import com.esms.common.entity.MTPack;
import com.husong.config.Config;

/**
 * 发送短信
 * 
 * @author maxmin
 *
 * @date 2015年11月9日 下午2:49:36
 */
public class BaseSMS {

	private static PostMsg pm;
	private static MTPack pack;
	private static Account ac;// 设置帐号密码 ;
	private static ArrayList<MessageData> msgs = new ArrayList<MessageData>();

	private static PostMsg getPostMsg() {
		if (pm == null) {
			pm = new PostMsg();
			pm.getCmHost().setHost("211.147.239.62", 8450);// 您设置的下行端口
			pm.getWsHost().setHost("211.147.239.62", 8460);// 您设置的下行端口
		}
		return pm;
	}

	private static MTPack getMTPack() {
		if (pack == null) {
			pack = new MTPack();
			pack.setBatchID(UUID.randomUUID());
			pack.setBatchName("JDK");
			pack.setMsgType(MTPack.MsgType.SMS);
			pack.setSendType(MTPack.SendType.MASS);
			pack.setBizType(0);
			pack.setDistinctFlag(false);
			pack.setCustomNum("");// 添加扩展码
		}
		return pack;
	}

	private static Account getAccount() {
		if (ac == null) {
			ac = new Account("njcbkd@njcbkd", "XW400xw!");
		}
		return ac;
	}

	private static void init() {
		ac = getAccount();
		pm = getPostMsg();
		pack = getMTPack();
	}

	public static int sendOne(String telephone, String code) {
		
		Account ac = new Account("njcbkd@njcbkd", "XW400xw!");// 设置帐号密码
		PostMsg pm = new PostMsg(); // 新建一个PostMsg对象

		pm.getCmHost().setHost("211.147.239.62", 8450);// 您设置的下行端口
		pm.getWsHost().setHost("211.147.239.62", 8460);// 您设置的下行端口

		MTPack pack = new MTPack();
		pack.setBatchID(UUID.randomUUID());
		pack.setBatchName("JDK");
		pack.setMsgType(MTPack.MsgType.SMS);
		pack.setSendType(MTPack.SendType.MASS);
		pack.setBizType(0);
		pack.setDistinctFlag(false);
		pack.setCustomNum("");// 添加扩展码

		ArrayList<MessageData> msgs = new ArrayList<MessageData>();

		// /* 以下是单发实例*/
		msgs.add(new MessageData(telephone, "呼送验证码：" + code + "。（3分钟有效）呼送，助您生活更轻松"));

		pack.setMsgs(msgs);
		try {
			GsmsResponse response = pm.post(ac, pack);
			String message = response.getMessage();
			int result = response.getResult();
			System.out.println(message+":"+result);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return -1;
//		init();
//		msgs.add(new MessageData(telephone, msg));
//		pack.setMsgs(msgs);
//		int result = 0;
//		UUID uuid =null;
//		try {
//			GsmsResponse response = pm.post(ac, pack);
//			String message = response.getMessage();
//			result = response.getResult();
//			
//			uuid = response.getUuid();
//			saveLog(result,telephone,uuid.toString(),msg);
//			System.out.println(message + ":" + result);
//		} catch (Exception e) {
//			e.printStackTrace();
//			saveLog(result,telephone,uuid.toString(),msg);
//		} finally {
//			if (msgs != null) {
//				msgs.clear();
//			}
//		}
//		return result;
	}

	public static int sendBatch(String[] nums, String msg) {
		init();
		for (String telephone : nums) {
			msgs.add(new MessageData(telephone, msg));
		}
		pack.setMsgs(msgs);
		int result = 0;
		try {
			GsmsResponse response = pm.post(ac, pack);
			result = response.getResult();
		} catch (Exception e) {
			e.printStackTrace();
			//保存错误日志
		} finally {
			if (msgs != null) {
				msgs.clear();
			}
		}
		return result;
	}

	/**
	 * 发送注册验证码
	 * @param telephone
	 * @param code
	 * @author maxmin
	 * @date 2015年11月10日上午11:51:22
	 */
	public static void sendRegistValicode(String telephone, String code) {
		 sendOne(telephone, "呼送验证码：" + code + "。（"+ Config.REGIST_VALICODE_EFFECTIVE_TIME + "分钟有效）呼送，助您生活更轻松");
	}
	/**
	 * 发送登录验证码
	 * @param telephone
	 * @param code
	 * @author maxmin
	 * @date 2015年11月10日上午11:51:12
	 */
	public static void sendLoginValicode(String telephone, String code) {
//		sendOne(telephone, "呼送验证码：" + code + "。（"+ Config.LOGIN_VALICODE_EFFECTIVE_TIME + "分钟有效）呼送，助您生活更轻松");
		
		sendOne(telephone, code);
	}
	
	public static void sendSMSBatch(String [] nums, String content) {
		sendBatch(nums, content);
	}
	
	private static void  saveLog(int ret,String telephone,String uuid,String content){
		String msg = "";
		switch (ret) {
		case 0:
			//等待发送
			msg="等待发送";
			break;
		case 1:
			//发送成功
			msg="发送成功";
			break;
		case 2:
			//被拒绝
			msg="被拒绝";
			break;
		case 3:
			//数据格式错误
			msg="数据格式错误";
			break;
		case 4:
			//多次发送失败
			msg="多次发送失败";
			break;
		case 5:
			//帧结束标志
			msg="帧结束标志";
			break;
		case 6:
			//序列号错误
			msg="序列号错误";
			break;
		case 7:
			//系统拒绝发送，可能是选择不到通道等情况
			msg="系统拒绝发送，可能是选择不到通道等情况";
			break;
		}
		if (ret!=1) {
			//执行保存日志的操作
			System.out.println(msg);
		}
	}

}
